package com.situ.ssm.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.situ.ssm.dao.IStudentDao;
import com.situ.ssm.entity.PageBean;
import com.situ.ssm.entity.Student;
import com.situ.ssm.service.IStudentService;
import com.situ.ssm.util.MyBatisUtil;
import com.situ.ssm.vo.StudentSearchCondition;

public class StudentServiceImpl implements IStudentService {
	IStudentDao studentDao = null;
	private SqlSession sqlSession;
	
	public  StudentServiceImpl() {
	sqlSession = MyBatisUtil.getSqlSession();
	studentDao = sqlSession.getMapper(IStudentDao.class);
	}
	
	public List<Student> list() {
		return studentDao.list();
	}

	public boolean insert(Student student) {
		Integer count = studentDao.insert(student);
		System.out.println(count);
		sqlSession.commit();
		return count == 1 ? true : false;
	}

	public boolean deleteById(Integer id) {
		Integer count = studentDao.deleteById(id);
		sqlSession.commit();
		return count == 1 ? true : false;
	}

	public Student findById(Integer id) {
		return studentDao.findById(id);
	}
	//修改
	public boolean update(Student student) {
		Integer count = studentDao.update(student);
		sqlSession.commit();
		return count == 1 ? true : false;
	}

	public boolean deleteAll(String[] selectIds) {
		Integer count = studentDao.deleteAll(selectIds);
		return count == 1 ? true : false;
	}

	public PageBean<Student> getPageBean(StudentSearchCondition condition) {
		PageBean<Student> pageBean = new PageBean<Student>();
		pageBean.setPageNo(condition.getPageNo());
		pageBean.setPageSize(condition.getPageSize());
		//总记录数（是符合condition条件的总记录数）
		int totalCount = studentDao.getTotalCount(condition);
		pageBean.setTotalCount(totalCount);
		//一共有多少页
		int totalPage = (int)Math.ceil((double)totalCount / condition.getPageSize());
		pageBean.setTotalPage(totalPage);
		//得到当前页的数据
		int offset = (condition.getPageNo() - 1) * condition.getPageSize();
		List<Student> list = studentDao.findPageBeanListByCondition(condition, offset);
		pageBean.setList(list);
		
		return pageBean;
	}
	//一对一查询
	public List<Student> oneVoneList() {
		return studentDao.oneVoneList();
	}

}
